package com.noteit.dao.impl;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.noteit.bean.Account;
import com.noteit.dao.LoginDAO;

public class LoginDAOImpl implements LoginDAO {
	private SessionFactory sessionFactory;
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public String checkAccountExist(String account) {
		Session session = sessionFactory.openSession();
		System.out.println("account in DAOImpl="+account);
		String hql = "select count(userName) from Account where deleteflag='N' and userName='"+account+"' ";
		Query query = session.createQuery(hql);
		
		String cnt = String.valueOf(query.list());
		
		session.close();
		return cnt;
	}
	
	public String saveAccountInfo(Account account)throws Exception {
		Session session = null;
		session = sessionFactory.openSession();
		try {
			Transaction t = session.beginTransaction();
			session.saveOrUpdate(account);
			t.commit();
			session.close();
			return "success";
		} catch (Exception e) {
			if (session.isOpen()) {
				session.close();
			}
			e.printStackTrace();
			return e.getMessage();
		}
	}
	
}
